package edu.iit.cs553;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class LocalWorker extends Thread {

	private List<Map<Integer, Long>> tasksPerThread;

	public LocalWorker(List<Map<Integer, Long>> tasksPerThread)
	{
		this.tasksPerThread = tasksPerThread;
		System.out.println("\nInstantiation of a worker");
		System.out.println("Task(s) associated :\n{<task_id>=<sleep_duration}");
		for (int i = 0; i < tasksPerThread.size(); i++)
		{
			System.out.println(tasksPerThread.get(i));
		}
	}

	@SuppressWarnings("static-access")
	@Override
	public void run()
	{
		try
		{
			if (!tasksPerThread.isEmpty())
			{
				for (Map<Integer, Long> task : tasksPerThread)
				{
					Set<Integer> keySet = task.keySet();
					Iterator<Integer> iterator = keySet.iterator();
					int taskKey = (Integer) iterator.next();
					long sleepDuration = task.get(taskKey);
					System.out.println("Starting sleep task #" + taskKey + ": " + sleepDuration + "ms" + " ie " + sleepDuration / 1000 + "s");
					Thread.currentThread().sleep(sleepDuration);
					System.out.println("Sleep task #" + taskKey + " completed");
				}
			}
		} catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
}
